package com.example.servlet.flight;

import com.example.service.FlightService;
import com.example.service.impl.FlightServiceImpl;
import com.example.utils.MybatisUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;

@WebServlet("/manager_add_flight")
public class mAddFlightServlet extends HttpServlet {
    private FlightService service;
    private SqlSession sqlSession;
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new FlightServiceImpl(sqlSession);
    }
    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String Airline = req.getParameter("Airline");
        String aircraftType = req.getParameter("aircraftType");
        String departureCity = req.getParameter("departureCity");
        String arrivalCity = req.getParameter("arrivalCity");
        String departureAirport = req.getParameter("departureAirport");
        String arrivalAirport = req.getParameter("arrivalAirport");
        String departureDate = req.getParameter("departureDate");
        String arrivalDate = req.getParameter("ArrivalDate");
        String departureTime = req.getParameter("departureTime");
        String arrivalTime = req.getParameter("arrivalTime");
        String flightStatus = req.getParameter("flightStatus");
        double economyPrice = Double.parseDouble(req.getParameter("economyPrice"));
        double businessPrice = Double.parseDouble(req.getParameter("businessPrice"));
        double firstPrice = Double.parseDouble(req.getParameter("firstPrice"));
        double distance = Double.parseDouble(req.getParameter("distance"));
        int Baggage = Integer.parseInt(req.getParameter("Baggage"));
        String mealMoney = req.getParameter("mealMoney");
        String mealType = req.getParameter("mealType");
        String gate = req.getParameter("gate");
        String buildDate = req.getParameter("buildDate");
        String WiFi = req.getParameter("WiFi");
        int economySeat = Integer.parseInt(req.getParameter("economySeat"));
        int businessSeat = Integer.parseInt(req.getParameter("businessSeat"));
        int firstSeat = Integer.parseInt(req.getParameter("firstSeat"));
        String captain = req.getParameter("captain");
        String assistant_captain = req.getParameter("assistant_captain");
        String chief_attendant = req.getParameter("chief_attendant");
        int flight_crew_num = Integer.parseInt(req.getParameter("flight_crew_num"));
        try{
            if(service.addPlane(aircraftType, Airline, buildDate, WiFi, economySeat+businessSeat+firstSeat, economySeat, businessSeat, firstSeat) == 0){
                throw new Exception("Failed to add plane");
            }
            int plane_id = service.getLastPlaneId();
            if(service.addFlight(aircraftType, plane_id, Airline, departureAirport, arrivalAirport, departureCity, arrivalCity, departureDate, departureTime, arrivalDate, arrivalTime, distance, economyPrice, businessPrice, firstPrice, Baggage, mealType, mealMoney, gate) == 0){
                throw new Exception("Failed to add flight");
            }
            int flight_id = service.getLastFlightId();
            if(service.addFlightStatus(flight_id, departureDate, captain, assistant_captain, chief_attendant, flight_crew_num, economySeat, businessSeat, firstSeat, flightStatus) == 0){
                throw new Exception("Failed to add flightStatus");
            }
            service.commit();
            req.getSession().setAttribute("add_success", true);
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            service.rollback();
            req.getSession().setAttribute("add_failure", new Object());
        }finally {
            destroy();
        }
        resp.sendRedirect("manager_flight");
    }
}
